#include "Map.h"
#include "GameCore\tinyxml2\tinyxml2.h"
void Map::Init(const char* name, const char* xmlSrc)
{
	currentWave = 0;
	rowElapsed = 0.0f;
	int len = strlen(xmlSrc);
	tinyxml2::XMLDocument doc;
	doc.Parse( xmlSrc );
	if(doc.ErrorID() != 0)
	{
		return;
	}
	tinyxml2::XMLElement* root = doc.FirstChildElement("root");
	tinyxml2::XMLElement* map = root->FirstChildElement("map");
	while(map)
	{
		const char* mapName = map->Attribute("name");
		if(strcmp(mapName, name) == 0)
		{
			rollSpeed = atof(map->Attribute("rollSpeed"));
			tinyxml2::XMLElement* waveXML = map->FirstChildElement("wave");
			while(waveXML)
			{
				Wave wave;
				wave.row = atof(waveXML->Attribute("row"));
				tinyxml2::XMLElement* shipXML = waveXML->FirstChildElement("ship");
				while(shipXML)
				{
					ShipInWave ship;
					ship.name = shipXML->Attribute("name");
					ship.x = atof(shipXML->Attribute("x"));
					ship.y = atof(shipXML->Attribute("y"));
					ShipInitialValue* siv = GameObjectFactory::GetShipInitialValueFromName(ship.name.c_str());
					if(siv)
					{
						ship.ship = (Ship*)siv->createFunction();
						ship.ship->SetSprite(SpriteManager::GetInstance()->LoadSpriteFromXMLFile(siv->spriteName));
						ship.ship->move.x = atof(shipXML->Attribute("mx"));
						ship.ship->move.y = atof(shipXML->Attribute("my"));
					}
					else
					{
						//error
					}
					wave.ships.push_back(ship);
					shipXML = shipXML->NextSiblingElement("ship");
				}
				this->waves.push_back(wave);
				waveXML = waveXML->NextSiblingElement("wave");
			}
			break;
		}
		map = map->NextSiblingElement("map");
	}
}